package Tread_and_Runnable;

/**
 *
 * 多线程的创建，方式一：继承于Thread类
 * 1.创建一个继承于Thread类的subclass
 * 2.重写thread类的run方法
 * 3.创建subclass对象
 * 4.调用start()
 * @author luyao_start
 * @create 2021-08-15-17:57
 */
class MyTread extends Thread{


    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if(i % 2 == 0){
                System.out.println(Thread.currentThread().getName()+":"+i);
            }

//            if(i % 20 == 0){
//                this.yield();
//            }
        }
    }

    public MyTread(String name) {
        super(name);
    }
}

public class ThreadTest {
    public static void main(String[] args) {
        MyTread myTread = new MyTread("线程一");

        Thread.currentThread().setName("主线程");

        myTread.start();

        for (int i = 0; i < 100; i++) {
            if(i % 2 == 0){
                System.out.println(Thread.currentThread().getName()+":"+i+"*****main*****");
            }

            if(i == 20){
                try {
                    myTread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
